import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_todo_2019/common/application.dart';
import 'package:flutter_todo_2019/common/common.dart';
import 'package:flutter_todo_2019/common/user.dart';
import 'package:flutter_todo_2019/event/login_event.dart';
import 'package:flutter_todo_2019/event/theme_change_event.dart';
import 'package:flutter_todo_2019/page/api/apis_service.dart';
import 'package:flutter_todo_2019/page/login/login_screen.dart';
import 'package:flutter_todo_2019/page/mode/base_model.dart';
import 'package:flutter_todo_2019/page/mode/user_info_model.dart';
import 'package:flutter_todo_2019/page/utils/styles.dart';
import 'package:flutter_todo_2019/page/utils/theme_util.dart';
import 'package:flutter_todo_2019/page/utils/toast_util.dart';
import 'package:flutter_todo_2019/page/utils/utils.dart';
import 'package:flutter_todo_2019/page/utils/sp_util.dart';
import 'package:flutter_todo_2019/page/utils/route_util.dart';

/// 侧滑页面
class DrawerScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new DrawerScreenState();
  }
}

class DrawerScreenState extends State<DrawerScreen>
    with AutomaticKeepAliveClientMixin {
  bool isLogin = false;
  String username = "去登录";
  String level = "--"; // 等级
  String rank = "--"; // 排名
  String myScore = ''; // 我的积分

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    this.registerLoginEvent();

    if (null != User.singleton.userName && User.singleton.userName.isNotEmpty) {
      isLogin = true;
      username = User.singleton.userName;
      getUserInfo();
    }
  }

  void registerLoginEvent() {
    Application.eventBus.on<LoginEvent>().listen((event) {
      setState(() {
        isLogin = true;
        username = User.singleton.userName;
        getUserInfo();
      });
    });
  }

  /// 获取用户信息
  Future getUserInfo() async {
    apiService.getUserInfo((UserInfoModel model) {
      if (model.errorCode == Constants.STATUS_SUCCESS) {
        setState(() {
          level = (model.data.coinCount ~/ 100 + 1).toString();
          rank = model.data.rank.toString();
          myScore = model.data.coinCount.toString();
        });
      }
    }, (DioError error) {});
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("设置",
            style: TextStyle(color: Colors.white, fontSize: 18)),
        centerTitle: true,

      ),
      body: ListView(
      padding: EdgeInsets.zero,
      children: <Widget>[
        Container(
          padding: EdgeInsets.fromLTRB(16, 40, 16, 10),
          color: Theme.of(context).primaryColor,
          child: Column(
            children: <Widget>[
              Container(
                alignment: Alignment.centerRight,
                child: InkWell(
                  child: Image.asset(
                    Utils.getImgPath('ic_rank'),
                    color: Colors.white,
                    width: 20,
                    height: 20,
                  ),
                  onTap: () {},
                ),
              ),
              CircleAvatar(
                backgroundImage:
                    AssetImage(Utils.getImgPath('ic_default_avatar')),
                radius: 40.0,
              ),
              Gaps.vGap10,
              InkWell(
                child: Text(
                  username,
                  style: TextStyle(fontSize: 20, color: Colors.white),
                ),
                onTap: () {
                  if (!isLogin) {
                    RouteUtil.push(context, LoginScreen());
                  }
                },
              ),
              Gaps.vGap5,
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text('等级:',
                      style: TextStyle(fontSize: 11, color: Colors.grey[100]),
                      textAlign: TextAlign.center),
                  Text(level,
                      style: TextStyle(fontSize: 11, color: Colors.grey[100]),
                      textAlign: TextAlign.center),
                  Gaps.hGap5,
                  Text('排名:',
                      style: TextStyle(fontSize: 11, color: Colors.grey[100]),
                      textAlign: TextAlign.center),
                  Text(rank,
                      style: TextStyle(fontSize: 11, color: Colors.grey[100]),
                      textAlign: TextAlign.center),
                ],
              )
            ],
          ),
        ),
        ListTile(
          title: Text(
            "我的积分",
            textAlign: TextAlign.left,
            style: TextStyle(fontSize: 16),
          ),
          leading: Image.asset(
            Utils.getImgPath('ic_score'),
            width: 24,
            height: 24,
            color: Theme.of(context).primaryColor,
          ),
          trailing: Text(myScore, style: TextStyle(color: Colors.grey[500])),
          onTap: () {},
        ),
        ListTile(
          title: Text(
            "我的收藏",
            textAlign: TextAlign.left,
            style: TextStyle(fontSize: 16),
          ),
          leading: Icon(Icons.favorite_border,
              size: 24, color: Theme.of(context).primaryColor),
          onTap: () {},
        ),
        ListTile(
          title: Text(
            "我的分享",
            textAlign: TextAlign.left,
            style: TextStyle(fontSize: 16),
          ),
          leading: Image.asset(
            Utils.getImgPath('ic_share'),
            width: 24,
            height: 24,
            color: Theme.of(context).primaryColor,
          ),
          onTap: () {},
        ),
        ListTile(
          title: Text(
            "TODO",
            textAlign: TextAlign.left,
            style: TextStyle(fontSize: 16),
          ),
          leading: Image.asset(
            Utils.getImgPath('ic_todo'),
            width: 24,
            height: 24,
            color: Theme.of(context).primaryColor,
          ),
          onTap: () {},
        ),
        ListTile(
          title: Text(
            "夜间模式",
            textAlign: TextAlign.left,
            style: TextStyle(fontSize: 16),
          ),
          leading: Icon(Icons.brightness_2,
              size: 24, color: Theme.of(context).primaryColor),
          onTap: () {
            setState(() {
              changeTheme();
            });
          },
        ),
        ListTile(
          title: Text(
            "系统设置",
            textAlign: TextAlign.left,
            style: TextStyle(fontSize: 16),
          ),
          leading: Icon(Icons.settings,
              size: 24, color: Theme.of(context).primaryColor),
          onTap: () {},
        ),
        Offstage(
          offstage: !isLogin,
          child: ListTile(
            title: Text(
              "退出登录",
              textAlign: TextAlign.left,
              style: TextStyle(fontSize: 16),
            ),
            leading: Icon(Icons.power_settings_new,
                size: 24, color: Theme.of(context).primaryColor),
            onTap: () {
              _logout(context);
            },
          ),
        )
      ],
    ));
  }

  /// 改变主题
  changeTheme() async {
    ThemeUtils.dark = !ThemeUtils.dark;
    SPUtil.putBool(Constants.DARK_KEY, ThemeUtils.dark);
    Application.eventBus.fire(new ThemeChangeEvent());
  }

  /// 退出登录
  void _logout(BuildContext context) {
    showDialog(
      context: context,
      builder: (context) => new AlertDialog(
        // title: new Text(''),
        content: new Text('确定退出登录吗？'),
        actions: <Widget>[
          new FlatButton(
            onPressed: () => Navigator.of(context).pop(false),
            child: new Text('取消', style: TextStyle(color: Colors.cyan)),
          ),
          new FlatButton(
            onPressed: () {
              requestLogOut(context);
            },
            child: new Text('确定', style: TextStyle(color: Colors.cyan)),
          ),
        ],
      ),
    );
  }

  requestLogOut(BuildContext context) {
    apiService.logout((BaseModel model) {
      Navigator.of(context).pop(true);
      if (model.errorCode == Constants.STATUS_SUCCESS) {
        User.singleton.clearUserInfo();
        T.show(msg: '已退出登录');
        Navigator.pop(context);
      } else {
        T.show(msg: model.errorMsg);
      }
    }, (DioError error) {
      print(error.response);
    });
  }
}
